package controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.Clothes;
import domain.Clothesout;
import domain.Order;
import domain.Orderdet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.OrderService;
import utils.DateGridViewResult;
import vo.Orderdetvo;
import vo.Ordervo;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : OrderController
 * @Description : //订单控制器类
 * @Author : LYH
 * @Date: 2022/6/4  20:28
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
//订单查找方法
    @RequestMapping("/orderin_search")
    @ResponseBody
    public DateGridViewResult orderin_search(Ordervo ordervo){
        System.out.println(ordervo.getPage());
        System.out.println(ordervo.getLimit());

        PageHelper.startPage(ordervo.getPage(),ordervo.getLimit());

        List<Order> orderList= orderService.findOrder_list(ordervo);

        PageInfo<Order> pageInfo = new PageInfo<>(orderList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
//入库订单增加方法
    @RequestMapping("/orderin_addOrder")
    @ResponseBody
    public Map<String,Object> addOrderin_order(Order order, HttpSession session){
        System.out.println(order.toString());
        int flag=orderService.insertorderin(order);
        Map<String,Object> map=new HashMap<>();
        if(flag>0){
            map.put("result",true);
            //将此订单信息存入session中，方便后续操作
            session.removeAttribute("order");
            session.setAttribute("order",order);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //入库订单明细添加操作
    @RequestMapping("/orderin_addOrderdet")
    @ResponseBody
    public Map<String,Object> addOrderin_orderdet(@RequestBody List<Orderdet> orderdetList,HttpSession session){
        int flag=0;
        //从数据库中获取到订单编号
        int o_id=orderService.selectOrder_id();
        System.out.println(o_id);
        //遍历List集合，对订单明细进行存储
        for (Orderdet orderdet : orderdetList) {
            System.out.println(orderdet);
            orderdet.setO_id(o_id);
            flag+=orderService.insertorderindet(orderdet);
        }
        Map<String,Object> map=new HashMap<>();
        if (flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }
    //订单信息修改方法
    @RequestMapping("/orderin_update")
    @ResponseBody
    public Map<String,Object> orderin_update(Order order){
        System.out.println(order.getO_id());
        System.out.println(order.getPerson());
        System.out.println(order.getOrigin());
        //接受数据库中影响行数，判断是否操作成功
        int flag=orderService.updateorderin(order);
        Map<String,Object> map=new HashMap<>();
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //订单明细查询方法
    @RequestMapping("/orderindet_search")
    @ResponseBody
    public DateGridViewResult orderindet_search(Orderdetvo orderdetvo){

        PageHelper.startPage(orderdetvo.getPage(),orderdetvo.getLimit());

        List<Orderdet> orderdetList= orderService.findOrderdet_List(orderdetvo.getO_id());

        PageInfo<Orderdet> pageInfo = new PageInfo<>(orderdetList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());

    }
    //订单删除方法
    @RequestMapping("/orderin_delete")
    @ResponseBody
    public Map<String,Object> orderin_delete(Order order){
        System.out.println(order.getO_id());
        int flag= orderService.deleteOrder(order);
        Map<String,Object> map=new HashMap<>();
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }
    //批量删除订单方法
    @RequestMapping("/order_deleteList")
    @ResponseBody
    public Map<String,Object> orderDeleteList(@RequestBody List<Order> orderList){
        Map<String,Object> map=new HashMap<>();
        int flag=0;

        for (Order order : orderList) {
            System.out.println(order.toString());
            flag=orderService.deleteOrder(order);
        }
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }
//出库订单查询方法
    @RequestMapping("/orderout_search")
    @ResponseBody
    public DateGridViewResult orderout_search(Ordervo ordervo){
        System.out.println(ordervo.getPage());
        System.out.println(ordervo.getLimit());

        PageHelper.startPage(ordervo.getPage(),ordervo.getLimit());

        List<Order> orderList= orderService.findOrderout_list(ordervo);

        PageInfo<Order> pageInfo = new PageInfo<>(orderList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
//增加出库订单方法
    @RequestMapping("/orderout_addOrder")
    @ResponseBody
    public Map<String,Object> addOrderout_order(Order order,HttpSession session){

        System.out.println(order.toString());
        int flag=orderService.insertorderout(order);
        Map<String,Object> map=new HashMap<>();
        if(flag>0){
            map.put("result",true);
            session.removeAttribute("order");
            session.setAttribute("order",order);
        }else {
            map.put("result",false);
        }
        return map;
    }
    //查询该仓库所有库存的方法
    @RequestMapping("/findtotal")
    @ResponseBody
    public DateGridViewResult findtotal(Clothesout clothesout,HttpSession session){
        Order order= (Order) session.getAttribute("order");
        String stock_name=order.getStock_name();
        System.out.println(stock_name);
        //获取到当前选择的仓库
        clothesout.setStock_name(stock_name);
        PageHelper.startPage(clothesout.getPage(),clothesout.getLimit());

        List<Clothesout> clothesoutList=orderService.findtotal(clothesout);
        PageInfo<Clothesout> pageInfo = new PageInfo<>(clothesoutList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());

    }
    //出库订单明细的添加方法
    @RequestMapping("/orderout_addOrderdet")
    @ResponseBody
    public Map<String,Object> Orderout_addOrderdet(@RequestBody List<Clothesout> clothesoutList,HttpSession session){
       Map<String,Object> map=new HashMap<>();
       Order order= (Order) session.getAttribute("order");
        System.out.println(order.toString());
        //从数据库中获取到订单ID
        int o_id=orderService.selectOrder_id();
        int flag=0;
        //判断输入的数字是否大于库存量
        for (Clothesout clothesout : clothesoutList) {
            if(clothesout.getAmount()>clothesout.getTotal()){
                map.put("result",false);
                return map;
            }

        }
        System.out.println(o_id);
        for (Clothesout clothesout : clothesoutList) {
            clothesout.setO_id(o_id);
            clothesout.setStock_name(order.getStock_name());
            flag=orderService.insertOrderoutdet(clothesout);
        }
        if (flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
    @RequestMapping("/findstock")
    @ResponseBody
    public DateGridViewResult findstock(Clothesout clothesout){


        PageHelper.startPage(clothesout.getPage(),clothesout.getLimit());

        List<Clothesout> clothesoutList=orderService.findstock(clothesout);
        PageInfo<Clothesout> pageInfo = new PageInfo<>(clothesoutList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());

    }

}
